package com.zhangzhan.gulimallcoupon.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhangzhan.gulimallcommcon.tos.SkuDiscountsEtc;
import com.zhangzhan.gulimallcommcon.util.CopyBean;
import com.zhangzhan.gulimallcommcon.util.PageUtils;
import com.zhangzhan.gulimallcommcon.util.Query;
import com.zhangzhan.gulimallcoupon.dao.SkuFullReductionDao;
import com.zhangzhan.gulimallcoupon.entity.MemberPriceEntity;
import com.zhangzhan.gulimallcoupon.entity.SkuFullReductionEntity;
import com.zhangzhan.gulimallcoupon.entity.SkuLadderEntity;
import com.zhangzhan.gulimallcoupon.service.MemberPriceService;
import com.zhangzhan.gulimallcoupon.service.SkuFullReductionService;
import com.zhangzhan.gulimallcoupon.service.SkuLadderService;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;


@Service("skuFullReductionService")
public class SkuFullReductionServiceImpl extends ServiceImpl<SkuFullReductionDao, SkuFullReductionEntity> implements SkuFullReductionService {

    @Resource
    private SkuLadderService skuLadderService;

    @Resource
    private MemberPriceService memberPriceService;


    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<SkuFullReductionEntity> page = this.page(
                new Query<SkuFullReductionEntity>().getPage(params),
                new QueryWrapper<SkuFullReductionEntity>()
        );

        return new PageUtils(page);
    }

    //// 6.4）、sku的优惠、满减等信息；guLimaLL_sms->sms_sku_Ladder\sms_sku_fuLL_reduction\sms_member_price
    @Override
    public void saveSkuDiscountsEtc(List<SkuDiscountsEtc> skuDiscountsEts) {
        if (CollUtil.isNotEmpty(skuDiscountsEts)) {
            for (SkuDiscountsEtc skuDiscountsEtc : skuDiscountsEts) {
                //sms_sku_Ladder
                if (skuDiscountsEtc.getFullCount() > 0) {
                    SkuLadderEntity skuLadderEntity = new SkuLadderEntity();
                    skuLadderEntity.setSkuId(skuDiscountsEtc.getSkuId());
                    skuLadderEntity.setFullCount(skuDiscountsEtc.getFullCount());
                    skuLadderEntity.setDiscount(skuDiscountsEtc.getDiscount());
                    skuLadderEntity.setAddOther(skuDiscountsEtc.getCountStatus());
                    skuLadderService.save(skuLadderEntity);
                }


                //sms_sku_fuLL_reduction
                if (skuDiscountsEtc.getFullPrice().compareTo(new BigDecimal(0)) > 0) {
                    SkuFullReductionEntity skuFullReduction = CopyBean.copyBean(skuDiscountsEtc, SkuFullReductionEntity.class);
                    skuFullReduction.setAddOther(skuDiscountsEtc.getPriceStatus());
                    this.save(skuFullReduction);
                }

                //sms_member_price
                if (CollUtil.isNotEmpty(skuDiscountsEtc.getMemberPrice())) {
                    List<MemberPriceEntity> memberPriceEntities = new ArrayList<>(skuDiscountsEtc.getMemberPrice().stream().map(po -> {
                        if (po.getPrice().compareTo(new BigDecimal(0)) > 0) {
                            MemberPriceEntity memberPrice = new MemberPriceEntity();
                            memberPrice.setSkuId(skuDiscountsEtc.getSkuId());
                            memberPrice.setMemberLevelId(po.getId());
                            memberPrice.setMemberLevelName(po.getName());
                            memberPrice.setMemberPrice(po.getPrice());
                            memberPrice.setAddOther(1);

                            return memberPrice;
                        }
                        return null;
                    }).toList());
                    memberPriceEntities.removeAll(Collections.singleton(null));
                    memberPriceService.saveBatch(memberPriceEntities);
                }
            }
        }
    }

}